import json
import unittest
import requests

# 10.59.9.18
class DianShang(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')
    def tearDown(self) -> None:
        print('case end')
    # 注册
    def test_01(self):
        r1 = requests.post(url='http://10.59.9.18/small/user/v1/register',
                           json={"phone": "17552719744", "pwd": "123456aA"}).json()
        print(r1)
    # 登录
    userid = None
    sessionid = None
    def test_02(self):
        r2 = requests.post(url='http://10.59.9.18/small/user/v1/login',
                           json={"phone": "17552719744", "pwd": "123456aA"}).json()
        print(r2)
        DianShang.userid = r2['result']['userId']
        DianShang.sessionid = r2['result']['sessionId']
    # 新增收货地址
    def test_03(self):
        r3 = requests.post(url='http://10.59.9.18/small/user/verify/v1/addReceiveAddress',
                           headers={'userid': str(DianShang.userid), "sessionid": str(DianShang.sessionid)},
                           params={"realName": "哈哈哈", "phone": "17552719744", "address": "北京 海淀区 八维", "zipCode": "101010"}).json()
        print(r3)
    # 商品信息列表
    commodityid = None
    price = None
    def test_04(self):
        r4 = requests.get(url='http://10.59.9.18/small/commodity/v1/commodityList').json()
        print(r4)
        DianShang.commodityid = r4['result']['pzsh']['commodityList'][0]['commodityId']
        DianShang.price = r4['result']['pzsh']['commodityList'][0]['price']
        print(DianShang.commodityid)
    # 商品详情
    def test_05(self):
        print(DianShang.commodityid)
        r5 = requests.get(url='http://10.59.9.18/small/commodity/v1/findCommodityDetailsById',
                          params={"commodityId": str(DianShang.commodityid)}).json()
        print(r5)

    # 地址列表
    addressId = None
    def test_06(self):
        r6 = requests.get(url='http://10.59.9.18/small/user/verify/v1/receiveAddressList',
                          headers={'userid': str(DianShang.userid), "sessionid": str(DianShang.sessionid)}).json()
        print(r6)
        DianShang.addressId = r6['result'][1]['id']
        print(DianShang.addressId)
    # 创建订单
    def test_07(self):
        h1 = json.dumps([{"commodityId": DianShang.commodityid,"amount":1}])
        r7 = requests.post(url='http://10.59.9.18/small/order/verify/v1/createOrder',
                           headers={'userid': str(DianShang.userid), "sessionid": str(DianShang.sessionid)},
                           data={"orderInfo": h1,
                                   "totalPrice": DianShang.price, "addressId": DianShang.addressId}).json()
        print(r7)
# if __name__ == '__main__':
#     # 第一种
    unittest.main()
#     suite = unittest.TestSuite()
#     suite.addTest()
#     unittest.TextTestRunner.run()
